#include "Passwords.h"
#include "PasswordsModel.h"
#include "MasterPassword.h"

#include <QSortFilterProxyModel>
#include <QMessageBox>

extern QString DecryptPassword(const QString &str);
extern QString EncryptPassword(const QString &str);

Passwords::Passwords(QWidget *parent)
    : QDialog(parent)
{
    setupUi(this);
    setWindowFlags(Qt::Sheet);

    s_show_passwords = false;

    PasswordsModel *model = new PasswordsModel(this);
    m_proxyModel = new QSortFilterProxyModel(this);
    connect(search, SIGNAL(textChanged(QString)),
            m_proxyModel, SLOT(setFilterFixedString(QString)));
    connect(removeButton, SIGNAL(clicked()), PasswordsTable, SLOT(removeOne()));
    connect(removeAllButton, SIGNAL(clicked()), PasswordsTable, SLOT(removeAll()));
    connect(buttonShowPasswords, SIGNAL(clicked()), this, SLOT(showPasswords()));
    connect(buttonMaster, SIGNAL(clicked()), this, SLOT(masterPassword()));
    m_proxyModel->setSourceModel(model);
    PasswordsTable->verticalHeader()->hide();
    PasswordsTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    PasswordsTable->setModel(m_proxyModel);
    PasswordsTable->setAlternatingRowColors(true);
    PasswordsTable->setTextElideMode(Qt::ElideMiddle);
    PasswordsTable->setShowGrid(false);
    PasswordsTable->setSortingEnabled(true);
    QFont f = font();
    f.setPointSize(10);
    QFontMetrics fm(f);
    int height = fm.height() + fm.height()/3;
    PasswordsTable->verticalHeader()->setDefaultSectionSize(height);
    PasswordsTable->verticalHeader()->setMinimumSectionSize(-1);
    for (int i = 0; i < model->columnCount(); ++i){
        int header = PasswordsTable->horizontalHeader()->sectionSizeHint(i);
        switch (i) {
        case 0:
            header = fm.width(QString(40, 'X'));
            break;
        case 1:
            header = fm.width(QString(12, 'X'));
            break;
        }
        int buffer = fm.width(QLatin1String("xx"));
        header += buffer;
        PasswordsTable->horizontalHeader()->resizeSection(i, header);
    }
    PasswordsTable->horizontalHeader()->setStretchLastSection(true);

}

Passwords::~Passwords()
{

}

void Passwords::showPasswords()
{
    s_show_passwords = !s_show_passwords;
    buttonShowPasswords->hide();
    PasswordsTable->update();
    PasswordsTable->selectAll();
    PasswordsTable->clearSelection();
}

void Passwords::masterPassword()
{
    QSettings settings;
    settings.beginGroup(QLatin1String("AutoComplete"));

    QString master = settings.value(QLatin1String("Master")).toString();

    MasterPassword *dialog = new MasterPassword(this, master);

    if (dialog->exec() == QDialog::Accepted)
    {
        if ( master.length() > 0 && DecryptPassword(master) != dialog->txtOld->text())
        {
            QMessageBox::warning(this, tr("Warning"), tr("Old password doesn't match the one you've typed!<br>New password has not been set."));
        }
        else
        {
            if (dialog->txtNew->text() != dialog->txtConfirm->text())
            {
                QMessageBox::warning(this, tr("Warning"), tr("Passwords do not match!<br>New password has not been set."));
            }
            else
            {
                QMessageBox::warning(this, tr("Success"), tr("New master password has been set."));
                settings.setValue(QLatin1String("Master"), EncryptPassword(dialog->txtNew->text()));
            }
        }
    }
    delete dialog;

}
